package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.domain.customer.CustomerDO;
import com.qk.dto.customer.CustomerDTO;
import com.qk.dto.customer.CustomerListDTO;
import com.qk.entity.Customer;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author: zyx
 * @created: 2025-10-16-15:55
 * @description:
 */
@Slf4j
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Override
    public void add(CustomerDTO dto) {
        //参数校验
        if(ObjectUtil.isEmpty(dto.getPhone())||ObjectUtil.isEmpty(dto.getName())){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        //补全参数
        Customer customer = BeanUtil.copyProperties(dto, Customer.class);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        this.save(customer);
    }

    @Override
    public PageResult<CustomerDO> selectPage(CustomerListDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<CustomerDO> page =this.baseMapper.selectByPage(dto);
        return PageResult.<CustomerDO>builder()
                .total((int) page.getTotal()).rows(page.getResult()).build();
    }
}
